package com.ld.control.business;

import com.alibaba.fastjson.JSONObject;
import com.ld.control.business.data.service.TemporaryMapService;
import com.ld.control.business.process.service.impl.ProcessNodeExampleServiceImpl;
import com.ld.control.business.sample.service.SampleService;
import com.ld.control.business.variable.service.VariableExampleService;
import com.ld.control.common.ResultResponse;
import com.ld.control.common.vo.GeneratePageListVo;
import com.ld.control.utils.CommonUtils;
import com.ld.control.utils.HttpUtils;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

@Slf4j
@RestController
public class TestCtrl {

    private final ReentrantLock lock = new ReentrantLock();

    @Autowired
    private ProcessNodeExampleServiceImpl processNodeExampleService;

    @Autowired
    private SampleService sampleService;

    @Autowired
    private VariableExampleService variableExampleService;

    @GetMapping("runDataExt1")
    public Object setVariableValueById(@RequestParam String param) {
        processNodeExampleService.runDataExt1(null);
        return  "";
    }

    @PostMapping("testPost")
    public Object testPost(@RequestBody String param) {
        return HttpUtils.sendPost("http://192.168.19.101", param);
    }

    @GetMapping("test")
    public Object test(Long id) {
        lock.lock();
        try {
            log.info("线程进来了");
            Thread.sleep(5000L);
            log.info("线程执行结束");
        } catch (Exception e) {

        }finally {
            lock.unlock();
        }
        return "success";
    }

    @GetMapping("testGetVariableExample")
    private Object testGetVariableExample(@RequestParam String param) {
        return variableExampleService.getByNameExt(9157L, param);
    }

    @GetMapping("test1")
    public Object test1(Long id) {
        String responseResult = "{\"Result\":[{\"hole\":10,\"phdata\":7.57,\"barcode\":\"\",\"temperature\":25.0},{\"hole\":11,\"phdata\":7.59,\"barcode\":\"\",\"temperature\":25.0},{\"hole\":12,\"phdata\":7.59,\"barcode\":\"\",\"temperature\":25.0},{\"hole\":13,\"phdata\":7.6,\"barcode\":\"\",\"temperature\":25.0},{\"hole\":14,\"phdata\":7.61,\"barcode\":\"\",\"temperature\":25.0},{\"hole\":15,\"phdata\":7.61,\"barcode\":\"\",\"temperature\":25.0}]}";
        JSONObject jsonObject = JSONObject.parseObject(responseResult);
        String data = jsonObject.getString("Result");
        List<Object> dataList = JSONObject.parseArray(data);
        String jsonString = JSONObject.toJSONString(dataList.get(0));
        JSONObject a = JSONObject.parseObject(jsonString);
        return a.getString("hole");
    }

    @GetMapping("/test2")
    @Operation(summary = "根据条件搜索样品列表")
    public ResultResponse<GeneratePageListVo<List<String>>> getPageList(
            @RequestParam(required = false) Long taskExampleId,
            @RequestParam(required = false) Long equipmentId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size
    ) {
        GeneratePageListVo<List<String>> pageList = sampleService.getPageListExt1(taskExampleId, equipmentId, current, size);
        return ResultResponse.success(pageList);
    }
}
